<?php

/**
 * Subclass for performing query and update operations on the 'hc_post' table.
 *
 *
 *
 * @package lib.model
 */
class PostPeer extends BasePostPeer
{

  /**
   * retourne un jeu de résultat de news suivant les paramètres passés
   * dans l'url.
   *
   * @param integer $limit nombre de résultats à retourner
   * @param string $lang langue du billet
   * @param integer year annee du post
   * @param integer month mois du post
   * @param integer day jour du post
   * @param string slug titre formaté du post
   *
   * @return object object de posts
   */
  static public function getNewsByParams($limit, $lang = 'fr', $year = null, $month = null, $day = null, $slug = null)
  {
     $c = new Criteria();
     $c->addDescendingOrderByColumn(PostPeer::PUBLISHED_AT);

     $c->add(PostPeer::IS_PUBLISHED, true, Criteria::EQUAL);

     if ($year)
     {
       $date = $year.'-';
       if ($month)
       {
         $date.= $month.'-';
         if ($day)
         {
           $date.= $day;

           if ($slug)
           {
              $c->add(PostPeer::SLUG, $slug, Criteria::EQUAL);
           }

         }
       }
       $criterion = $c->getNewCriterion(PostPeer::PUBLISHED_AT, $date.'%', Criteria::LIKE);
       $criterion->addAnd($c->getNewCriterion(PostPeer::PUBLISHED_AT , date('Y-m-d H:i:s'), Criteria::LESS_THAN ));
       $c->add($criterion);
     }
     else
     {
       $c->add(PostPeer::PUBLISHED_AT , date('Y-m-d H:i:s'), Criteria::LESS_THAN);
     }
 
     $c->addJoin(PostPeer::LANG_ID, LangPeer::ID);
     $c->add(LangPeer::FLAG, $lang, Criteria::EQUAL);

     $c->setLimit($limit);

     $news = $slug ? PostPeer::doSelectOne($c) : PostPeer::doSelect($c);

     return $news;
  }

  /**
   * function qui retourne un tableau associatif de la liste des mois
   * d'archives disponibles + un lien vers cette archive
   *
   * @return array tableau d'archives
   */
  static public function getListOfArchives()
  {
    $c = Propel::getConnection();
    $q = 'SELECT DISTINCT(substring(%s,1,7)) as date FROM %s, hc_lang ';
    $q.= 'WHERE %s < "'. date('Y-m-d H:i:s') .'" ';
    $q.= 'AND hc_lang.id = hc_post.lang_id ';
    $q.= 'AND hc_lang.flag = "'.sfContext::getInstance()->getUser()->getCulture().'" ';
    $q.= 'ORDER BY date desc';
    $q = sprintf($q, PostPeer::PUBLISHED_AT, PostPeer::TABLE_NAME, PostPeer::PUBLISHED_AT);
    $s = $c->prepareStatement($q);
    $result = $s->executeQuery();

    $arrArchives = array();
    $i = 0;
    while($i < $result->getRecordCount())
    {
      $result->next();

      preg_match('/(\d{4})-(\d{2})/', $result->getString('date'), $matches, PREG_OFFSET_CAPTURE);
      $arrArchives[$i] = array(
       'monthLabel' => self::getFormatMonth($matches[2][0]),
       'month' => $matches[2][0],
       'year' => $matches[1][0]);
      $i++;
    }
        
    return $arrArchives;
  }

  static public function getFormatMonth($month)
  {
    $arrMonths = array(
      '01' => 'January',
      '02' => 'February',
      '03' => 'March',
      '04' => 'April',
      '05' => 'May',
      '06' => 'June',
      '07' => 'July',
      '08' => 'August',
      '09' => 'September',
      '10' => 'October',
      '11' => 'November',
      '12' => 'December');

     return (array_key_exists($month, $arrMonths)) ? $arrMonths[$month] : false;

  }

  /**
   * TODO: ajouter la gestion de la langue
   */
  static public function getArchives()
  {
    $arrArchives = self::getListOfArchives();
    if (is_array($arrArchives))
    {
      foreach ($arrArchives as $archive)
      {
        $r[] = array(
         'title' => self::getFormatMonth($archive['month']).' '.$archive['year'],
         'link' => 'news/listByParams?year='.$archive['year'].'&month='.$archive['month']
         );

      }
    }

    return $r;
  }

}
